﻿#include "pinsinfors.h"
#include <QApplication>
#include <QFile>
#include <QDebug>

PinsInfors  * PinsInfors::m_pinsInfors = NULL;
PinsInfors::PinsInfors(QObject *parent) : QObject(parent)
{
    initPin();
}

PinsInfors::~PinsInfors()
{

}

void PinsInfors::initPin()
{
    m_pinData.clear();
    QString docParhFile =  QApplication::applicationDirPath();
    docParhFile +="/configs/pinsinfors.json";

    QFile files(docParhFile);
    if(files.open(QIODevice::ReadOnly)){
        QString inforsa = files.readAll();
        QJsonParseError errors;
        QJsonDocument doc = QJsonDocument::fromJson(inforsa.toUtf8(),&errors);
        if(errors.error == QJsonParseError::NoError){
            QJsonArray arrya = doc.array();
            for(int i = 0 ; i < arrya.size() ;i++ ){
                QJsonObject itema = arrya.at(i).toObject();
#ifdef DEBUG_INFOR
                qDebug()<< metaObject()->className()<<__func__<<__LINE__<<itema;
#endif
                // int user_pin = itema.take("user_pin").toInt();
                int pins =  itema.take("pins").toInt();
                QString speed =  itema.take("speed").toString();
                QString direction ="0";
                if(itema.contains("direction")){
                    direction = itema.take("direction").toString();

                }
                QString compenTime ="0";
                if(itema.contains("compenTime")){
                    compenTime = itema.take("compenTime").toString();

                }
                QString remindHeight ="0";
                if(itema.contains("remindHeight")){
                    remindHeight = itema.take("remindHeight").toString();

                }
                QString remindLed ="0";
                if(itema.contains("remindLed")){
                    remindLed = itema.take("remindLed").toString();

                }
                QString minElectric ="1";
                if(itema.contains("minElectric")){
                    minElectric = itema.take("minElectric").toString();

                }
                QString maxElectric ="1150";
                if(itema.contains("maxElectric")){
                    maxElectric = itema.take("maxElectric").toString();

                }

                int isValues = 1;
                if(itema.contains("isValues")){
                    isValues = itema.take("isValues").toInt();

                }
                QString minHeight =  itema.take("minHeight").toString();
                QString maxHeight =  itema.take("maxHeight").toString();
                PinData * pinData = new PinData();
                pinData->m_pins = pins;
                pinData->m_speed = speed;
                pinData->m_direction = direction;
                pinData->m_compenTime = compenTime;
                pinData->m_minHeight = minHeight;
                pinData->m_maxHeight = maxHeight;
                pinData->m_remindHeight = remindHeight;
                pinData->m_remindLed = remindLed;
                pinData->m_isValues = isValues;
                pinData->m_minElectric = minElectric;
                pinData->m_maxElectric = maxElectric;
                m_pinData.insert(pins,pinData);
            }
        }
        files.close();
    }
#ifdef DEBUG_INFOR
    qDebug()<<__func__<<__LINE__<<m_pinData.keys();
#endif
}

PinsInfors *PinsInfors::gestance()
{
    if(m_pinsInfors == NULL){
        m_pinsInfors = new PinsInfors();
    }

    return  m_pinsInfors;
}

PinData * PinsInfors::getPin(int pins)
{
    PinData * pinData =NULL;
    pinData = m_pinData.value(pins,pinData);
    return pinData;
}

QList<int> PinsInfors::keyPins()
{
    return m_pinData.keys();
}
